package com.chem.lems.controller;

import com.chem.lems.entity.User;
import com.chem.lems.serves.RoleServies;
import com.chem.lems.serves.UserServies;
import com.chem.lems.utils.ResultUtil;
import com.chem.lems.vo.PassVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServies userServies;
    @Autowired
    private RoleServies roleServies;


    /**
     * 获取全部用户列表
     *
     * @return
     */
    @GetMapping("/users")
    public ResultUtil usersList() {
        return userServies.gitUserByAll();
    }

    /**
     * 获取全部用户列表 分页
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/users/{page}/{size}")
    public ResultUtil usersList(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return userServies.gitUserByAll(page, size);
    }

    /**
     * 获取全部学生列表 分页
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/students/{page}/{size}")
    public ResultUtil studentsList(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return userServies.gitstudentsListByAll("3", page, size);
    }

    /**
     * 模糊查询用户列表 分页
     *
     * @param username
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/users/{username}/{page}/{size}")
    public ResultUtil usersList(@PathVariable("username") String username, @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return userServies.gitUserByNameLike(username, page, size);
    }


    /**
     * 模糊查询学生列表 分页
     *
     * @param username
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/students/{username}/{page}/{size}")
    public ResultUtil studentsList(@PathVariable("username") String username, @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return userServies.gitstudentsByNameLike(username, page, size);
    }
    /**
     * 根据用户名用户名查询用户信息
     *
     * @param username
     * @return
     */
    @GetMapping("name/{username}")
    public ResultUtil userInfoByname(@PathVariable("username") String username) {
        return userServies.gitUserByName(username);
    }

    /**
     * 根据用户名ID查询用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("{userId}")
    public ResultUtil userInfoByid(@PathVariable("userId") String userId) {
        return userServies.gitUserByUserId(userId);
    }

    /**
     * 更新用户状态
     *
     * @param username
     * @param state
     * @return
     */
    @PutMapping("{userId}/{state}")
    public ResultUtil updataState(@PathVariable("userId") String username, @PathVariable("state") boolean state) {
        return userServies.updataUserState(username, state);
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @PutMapping()
    public ResultUtil editUser(@RequestBody User user) {
        return userServies.editUserInfo(user);
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @DeleteMapping("{userId}")
    public ResultUtil deleteById(@PathVariable("userId") String userId) {
        return userServies.deleteById(userId);
    }


    /**
     * 新增用户
     *
     * @param userFrom
     * @return
     */
    @PostMapping()
    public ResultUtil addUser(@RequestBody User userFrom) {
        return userServies.addUser(userFrom);
    }


    /**
     * 获取所有的角色列表
     *
     * @return
     */
    @GetMapping("/roleinfo")
    public ResultUtil gitAllRoleInfo() {
        return roleServies.gitAllRoleInfo();
    }

    /**
     * 给用户分配角色
     *
     * @param userId
     * @param roleId
     * @return
     */
    @PutMapping("/role/{userId}/{roleId}")
    public ResultUtil UserAuthRole(@PathVariable("userId") String userId, @PathVariable("roleId") String roleId) {
        return userServies.UserAuthRole(userId, roleId);
    }

    /**
     * 用户密码修改
     * @param userId
     * @param passVO
     * @return
     */
    @PostMapping("/pass/{userId}")
    public ResultUtil ChangePasswd(@PathVariable("userId") String userId, @RequestBody PassVO passVO) {
        return userServies.ChangePasswd(userId, passVO);
    }
}

